/**
 * tzdesk系统平台
 * moonvip_admin
 * com.tz.service.cashapply
 * ICashApplyService.java
 * 创建人:小胡
 * 时间：2017年01月24日 09:23:34
 *
 */
package com.tz.service.cashapply.impl;

import com.tz.bean.*;
import com.tz.dao.cashapply.ICashApplyMapper;
import com.tz.dao.paymentdetail.IPaymentdetailMapper;
import com.tz.dao.user.IUserMapper;
import com.tz.dao.wallet.IWalletMapper;
import com.tz.service.cashapply.ICashApplyService;
import com.tz.util.StringHelper;
import com.tz.util.TmStringUtils;
import com.tz.util.weixinUtils.pay.PayCommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提现申请管理serviceImpl
 * ICashApplyService
 * 创建人:小胡
 * 时间：2017年01月24日 09:23:34
 * @version 1.0.0
 *
 */
@Service
public class CashApplyServiceImpl implements ICashApplyService{

	@Autowired
	private ICashApplyMapper cashapplyMapper;

	@Resource
	private IWalletMapper walletMapper;

	@Resource
	private IPaymentdetailMapper paymentdetailMapper;

	@Resource
	private IUserMapper userMapper;

	@Override
	public List<HashMap<String ,Object>> findCashApplys(TzParams params){
		if(TmStringUtils.isEmpty(params.getOrder()))params.setOrder("c.create_time desc");
		return cashapplyMapper.findCashApplys(params);
	}

	@Override
	public int count(TzParams params) {
		return cashapplyMapper.count(params);
	}

	@Override
	public HashMap<String ,Object> getCashApply(Integer id) {
		return cashapplyMapper.getCashApply(id);
	}

	@Override
	public int save(CashApply cashapply) {
		return cashapplyMapper.save(cashapply);
	}

	@Override
	public int update(CashApply cashapply) {
		return cashapplyMapper.update(cashapply);
	}

	@Override
	public int delete(TzParams params) {
		return cashapplyMapper.delete(params);
	}

	/**
	 * 提现申请通过
	 */
	//查询前台用户的余额和账户
	//判断是否提现金额小于余额，如果是则提现成功，如果否，则提现失败，告知不能通过
	//如果满足以上条件，则①修改更新提现记录表的处理状态和处理人
	//②更新虚拟账户金额，减去对应的提现金额
	//③新增虚拟账户明细表记录
	//④调用接口。企业付款到个人用户的零钱（需要查询用户的openid）
	public Map<String,Object> updateAgree(Integer id ,Integer frontuserId ,Double money, String backUsername ,HttpServletRequest request){
		Map resultmap = new HashMap();
		//查询前台用户的余额和账户
		Wallet wallet = walletMapper.getWalletByUserId(frontuserId);
		Double balance = wallet.getBalance();
		//判断是否提现金额小于余额，如果是则提现成功，如果否，则提现失败，告知不能通过
		if(money>balance){
			resultmap.put("res","overBalance");
		}
		if(money>20000){
			resultmap.put("res","once2000");
		}
		if(money<1){
			resultmap.put("res","limit1");
		}
		//④调用接口。企业付款到个人用户的零钱（需要查询用户的openid）
		//查询openid
		User user = userMapper.getUserInfoById(frontuserId);
		String openid = user.getOpenid();
		//生成订单号
		String orderno = StringHelper.getOrderNum();
		//调用付款接口
		BigDecimal bmoney = BigDecimal.valueOf(money);
		Map<String, String> paymap = null;
		Map<String, String> map = null;
		try{
			paymap = PayCommonUtil.weixinEnterprisePayment(orderno,bmoney,"提现",openid,request);
			resultmap.put("paymap",paymap);
		}catch (Exception e1){
			e1.printStackTrace();
			resultmap.put("res","fail1");
		}
		try{
			//调用查询接口
			map = PayCommonUtil.queryWeixinEnterprisePayment(orderno);
			resultmap.put("map",map);
		}catch (Exception e2){
			e2.printStackTrace();
			resultmap.put("res","fail2");
		}
		if("SUCCESS".equals(map.get("return_code")) && "SUCCESS".equals(map.get("status")) ){
			BigDecimal bm = bmoney.multiply(new BigDecimal(100));
			int m= bm.intValue();
			int getM = Integer.parseInt(map.get("payment_amount"));
			if(m == getM){
				//①修改更新提现记录表的处理状态和处理人
				CashApply cashApply = new CashApply();
				cashApply.setUpdateTime(new Date());
				cashApply.setDealcode("002");  //001 申请中 002已通过 003已驳回
				cashApply.setOpname(backUsername);
				cashApply.setId(id);
				cashapplyMapper.update(cashApply);
				//②更新虚拟账户金额，减去对应的提现金额
				Double newBalance = balance - money;
				wallet.setBalance(newBalance);
				wallet.setUpdateTime(new Date());
				walletMapper.update(wallet);
				//③新增虚拟账户明细表记录
				Paymentdetail paymentdetail = new Paymentdetail();
				paymentdetail.setWalletNo(wallet.getWalletno());
				paymentdetail.setChangeMoney(-money);
				paymentdetail.setBalance(newBalance);
				paymentdetail.setMemo("用户提现，操作人："+backUsername);
				paymentdetailMapper.save(paymentdetail);
				resultmap.put("res","success");
			}else{
				resultmap.put("res","fail");
			}
		}
		return resultmap;
	}


	public static void main(String[] args) {
//		   Double d = 1.0;
//		System.out.println(BigDecimal.valueOf(d));

		//数字字符串
		String StrBd="100";
		//构造以字符串内容为值的BigDecimal类型的变量bd
		BigDecimal bd=new BigDecimal(StrBd);
		int m= bd.intValue();
		System.out.println(m);
	}


}
